package xmltomce;
import static org.junit.Assert.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;


public class XmlDataMatcherTest {
	ArrayList<Movie> movies;
	String testFolderRoot = "D:\\JavaWorkspace\\MovieCollectorToMce\\testdata\\";
	String dvdFolderPath;
	
	@Before
	public void setUp() throws Exception {
		dvdFolderPath = testFolderRoot + "dvds";
		File dvdFolder = new File(dvdFolderPath);
		File xmlData = new File(testFolderRoot+"allMovies.xml");
		XmlDataMatcher xdm = new XmlDataMatcher();
		movies = xdm.getMoviesWithXmlDescription(dvdFolder, xmlData);
	}

	@Test
	public void testDvdFolderHaveMathingXml() {
		ArrayList<File> dvdFolders = getDvdFolders();
		assertEquals(movies.size(), dvdFolders.size());
		
		Iterator<Movie> itM = movies.iterator();
		Iterator<File> itF = dvdFolders.iterator();
		while(itM.hasNext()){
			Movie movie = itM.next();
			File folder = itF.next();
			assertEquals(movie.getTitle(), folder.getName());
		}
	}
	
	@Test
	public void testXmlDataHasValidContent(){
		Movie alphaDog = movies.get(0);
		assertEquals("Alpha Dog", alphaDog.getTitle());
		assertEquals(9072, alphaDog.getXmlId());
	}
	
	@Test
	public void testXmlDataIsInDvdFolder(){
		Iterator<Movie> it = movies.iterator();
		while(it.hasNext()){
			Movie m = it.next();
			DvdFolderXmlWriter dfxw = new DvdFolderXmlWriter(m);
			dfxw.saveXmlFile();
			
			File xmlFile = new File(m.getDvdFolder().getAbsolutePath() + "\\"+m.getTitle()+".dvdid.xml");
			assertTrue(xmlFile.isFile());
		}
	}
	
	@Test
	public void testXmlDataIsInInfoCache(){
		File infoCacheFolder = new File(testFolderRoot+"mceXmlStorage");
		Iterator<Movie> it = movies.iterator();
		while(it.hasNext()){
			Movie m = it.next();
			InfoCacheXmlWriter icxw = new InfoCacheXmlWriter(infoCacheFolder);
			icxw.saveXmlFile(m);
			
			File xmlFile = new File(infoCacheFolder.getAbsolutePath()+"\\"+m.getXmlId()+".xml");
			assertTrue(xmlFile.isFile());
		}
	}
	
	private ArrayList<File> getDvdFolders(){
		ArrayList<File> dvdFolders = new ArrayList<File>();
		File rootFolder = new File(dvdFolderPath);
		File[] folders = rootFolder.listFiles();
		for (File dvdFolder : folders){
			dvdFolders.add(dvdFolder);
		}
		return dvdFolders;
	}

}
